package com.ihr360.authority.sharing.validator;

import com.ihr360.authority.sharing.service.AuthUserResourceService;
import com.ihr360.commons.data.util.Pair;
import com.ihr360.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author davidwei
 */
@Component
public class GetOneValidator implements AuthValidator<Object> {
    @Autowired
    private AuthUserResourceService authUserResourceService;

    @Override
    public boolean supports(Class<?> clazz) {
        return true;
    }

    @Override
    public void validate(Object target, Class authEntity) {
        ValidationUtils.rejectIfEmpty(target, null, "您没有权限获取此数据");
        Pair<Integer, Integer> pair = authUserResourceService.queryUserResourcePrivilege(authEntity, target.toString());
        Integer privilege = null;
        Integer publicity = null;
        if (pair != null) {
            privilege = pair.getFirst();
            publicity = pair.getSecond();
        }
        ValidationUtils.rejectIfEmpty(privilege, null, "您没有权限获取此数据");
        ValidationUtils.rejectIfZero(Long.valueOf(privilege), null, "您没有权限获取此数据");
    }
}